package cn.com.duiba.millionaire.center.biz.service.impl;

import cn.com.duiba.millionaire.center.api.enums.ActivityTypeEnum;
import cn.com.duiba.millionaire.center.api.enums.GameAnswerEnums;
import cn.com.duiba.millionaire.center.api.msg.SubmitAnswerMsg;
import cn.com.duiba.millionaire.center.biz.service.AnswerHanderService;
import cn.com.duiba.millionaire.center.biz.service.QuestionSelectionStastisticService;
import cn.com.duiba.millionaire.center.biz.service.UserSelectionInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ExecutorService;

/**
 * Created by zzy on 2018/1/22.
 */
@Service
public class AnswerHanderServiceImpl extends AbstractAnswerHanderService implements AnswerHanderService {
    @Autowired
    private QuestionSelectionStastisticService questionSelectionStastisticService;
    @Autowired
    private UserSelectionInfoService userSelectionInfoService;

    @Autowired
    private ExecutorService executorService;

    @Override
    public void processMsgList(List<SubmitAnswerMsg> msgList) {
        //key1:活动id,key2:questionIndex,key3:userAnswer(用户选择的答案)，value3:选择该答案的人数
        Map<Long, Map<Integer, Map<String, Integer>>> activitySelectionMap = new LinkedHashMap<>();
        //存放答对的用户答案信息
        List<SubmitAnswerMsg> correctAnswerMsgList = new ArrayList<>();
        //存放答错的用户信息
        List<SubmitAnswerMsg> wrongAnswerMsgList = new ArrayList<>();
        for (SubmitAnswerMsg submitAnswerMsg : msgList) {
            Integer questionIndex = submitAnswerMsg.getQuestionIndex();
            Integer actType = submitAnswerMsg.getActType();
            Long actId = submitAnswerMsg.getActivityId();
            String userAnswer = submitAnswerMsg.getUserAnswer();
            // 判断答案是否正确
            if (isCorrect(actId, questionIndex, userAnswer,actType)) {
                correctAnswerMsgList.add(submitAnswerMsg);
            } else {
                wrongAnswerMsgList.add(submitAnswerMsg);
            }
            Map<Integer, Map<String, Integer>> questionSelections = activitySelectionMap.get(actId);
            if (questionSelections == null) {
                questionSelections = new LinkedHashMap<>();
                activitySelectionMap.put(actId, questionSelections);
            }
            Map<String, Integer> selections = questionSelections.get(questionIndex);
            if (selections == null) {
                selections = new HashMap<>();
                questionSelections.put(questionIndex, selections);
            }
            Integer count = selections.get(userAnswer);
            int tempValue = 1;
            if (count != null) {
                tempValue = count + 1;
            }
            selections.put(submitAnswerMsg.getUserAnswer(), tempValue);
        }
        executorService.execute(()->processWrongAnswer(wrongAnswerMsgList));
        executorService.execute(()->processCorrectAnswer(correctAnswerMsgList));
        // 选项计数
        questionSelectionStastisticService.batchAdd(activitySelectionMap);
        // 批量增加用户选择
        userSelectionInfoService.batchUpdate(msgList, true);

    }

    /**
     * 判断是否是正确答案
     *
     * @param activityId
     * @param questionIndex
     * @param answer
     * @return
     */
    private boolean isCorrect(Long activityId, Integer questionIndex, String answer,Integer actType) {
        //游戏判断答案方式
        if(actType != null && actType == ActivityTypeEnum.GAME.getType()){
            return Objects.equals(answer, GameAnswerEnums.CORRECT.getAnswer());
        }
        //答题判断方式
        return Objects.equals(answer, getQuestionPair(activityId, questionIndex).getValue0());
    }

    private void processCorrectAnswer(List<SubmitAnswerMsg> correctAnswerMsgList) {
        addWinnerList(correctAnswerMsgList);
    }

}
